package com.iss.lxhproject.controller;

import com.iss.lxhproject.entity.Guest;
import com.iss.lxhproject.model.MyResponseData;
import com.iss.lxhproject.service.GuestService;
import com.iss.lxhproject.service.RoomfreeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("guest/")
public class GuestController {
    @Autowired
    private GuestService service;
    @Autowired
    private RoomfreeService roomfreeService;
    //添加登记客户信息
    @RequestMapping("insertGuest")
    public MyResponseData insertGuest(Guest guest){
        MyResponseData responseData = new MyResponseData();
        Integer count=null;
        try {
             count = service.insertGuest(guest);
            System.out.println(count);
        } catch (Exception e){
            responseData.setErrorCode(-2);
            responseData.setStatus("录入信息不能为空");
        }
        String state = roomfreeService.selectRoomState(guest.getGuest_roomid());
        Integer count1 = roomfreeService.updateRoomState("已入住",guest.getGuest_roomid());
        if(state.equals("已入住")){
            responseData.setStatus(guest.getGuest_roomid()+"房间已满");
            responseData.setErrorCode(-1);
        }else {
            if (count >0 && count1>0) {
                responseData.setErrorCode(0);
                responseData.setObject(guest);
                responseData.setStatus("插入成功");
            }else {
                responseData.setErrorCode(-2);
                responseData.setStatus("插入失败");
            }
        }
        return responseData;
    }
    @RequestMapping("selectAllGuest")
    public List<Guest> selectAllGuest(){
        List<Guest> guest = service.selectAllGuest();
        //System.out.println(guest);
        return guest;
    }

    @RequestMapping("insertGuestforGuestlist")
    public MyResponseData insertGuestforGuestlist(Guest guest){
        MyResponseData responseData = new MyResponseData();
            Integer count = service.insertGuestforGuestlist(guest);
            if (count > 0) {
                responseData.setErrorCode(0);
                responseData.setStatus("添加成功");
            } else {
                responseData.setErrorCode(-1);
                responseData.setStatus("添加失败");
            }
        return responseData;
    }

//退房操作
    @RequestMapping("checkOutRoom")
    public MyResponseData checkOutRoom(String[] checkout_room){
        MyResponseData responseData = new MyResponseData();
        System.out.println("Checkout_room======="+checkout_room[0]);
        try{
            if (checkout_room[0]!=null){
                int count =roomfreeService.updateRoomState1(checkout_room);
                System.out.println("count======="+count);
                if (count>0){
                    responseData.setErrorCode(0);
                    responseData.setStatus("退房成功!");
                }else{
                    responseData.setErrorCode(-1);
                    responseData.setStatus("退房失败!");
                }
            }else{
                responseData.setErrorCode(-1);
                responseData.setStatus("请先选中房间!");
            }
        }catch(Exception e){
            System.out.println("我是NullPointerException");
            responseData.setErrorCode(-1);
            responseData.setStatus("请先选中房间!");
        }
        return responseData;
    }


    //更新编辑操作
    @RequestMapping("updateGuest")
    public MyResponseData updateGuest(Guest guest){
        MyResponseData responseData = new MyResponseData();
        Integer count = service.updateGuest(guest);
        if(count>0){
            responseData.setErrorCode(0);
            responseData.setStatus("更新客户信息成功");
        }else{
            responseData.setErrorCode(-1);
            responseData.setStatus("更新失败");
        }
        return responseData;
    }


    //删除操作
    @RequestMapping("deleteGuest")
    public MyResponseData deleteGuest(Integer[] guest_id){
        MyResponseData responseData = new MyResponseData();
        try {
            Integer count = service.deleteGuest(guest_id);
            if(count>0){
                responseData.setErrorCode(0);
                responseData.setStatus("删除成功");
            }else{
                responseData.setErrorCode(-1);
                responseData.setStatus("删除失败");
            }
        }catch (Exception e){
            responseData.setErrorCode(-1);
            responseData.setStatus("请选择要清除的客户信息");
        }
        return responseData;
    }
    @RequestMapping("selectGuestById")
    public MyResponseData selectGuestById(Integer guest_id){
        MyResponseData responseData = new MyResponseData();
        Guest guest = service.selectGuestById(guest_id);
        if(guest!=null){
            responseData.setErrorCode(0);
            responseData.setStatus("success");
            responseData.setObject(guest);
        }
        return responseData;
    }
}
